/* eslint-disable */
const fs = require('fs');
const path = require('path');

const CONFIG_FILE = 'mock.config.js';

const getConfigData = () => {
  try {
    const rawMockConfig = fs
      .readFileSync(path.resolve(process.cwd(), CONFIG_FILE))
      .toString('utf-8');
    const fn = new Function('module', `${rawMockConfig}; return module.exports;`);
    return fn({});
  } catch {
    return {};
  }
};

const bypass = (req, res, proxyOptions) => {
  const mockConfig = getConfigData();
  if (req.url in mockConfig) {
    res.setHeader('Content-Type', 'application/json');
    res.end(JSON.stringify(mockConfig[req.url]));
  }
  return null;
};

module.exports = {
  bypass: bypass,
};